package com.softroad.help.app.ui;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import net.arnx.jsonic.JSON;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.softroad.help.app.AppManager;
import com.softroad.help.app.R;
import com.softroad.help.app.bean.StatusResponse;
import com.softroad.help.app.constant.Apis;
import com.softroad.help.app.handler.StringHandler;
import com.softroad.help.app.tools.DialogUtils;
import com.softroad.help.app.tools.FileUtil;
import com.softroad.help.app.tools.HttpTool;
import com.softroad.help.app.tools.StringTool;
import com.softroad.help.app.tools.ToastTool;

public class SubmitPhotoActivity extends BaseActivity implements
		OnClickListener {

	private LinearLayout mine_head;
	private ImageView head_img;
	private EditText titlEditText;
	private EditText contentEditText;

	private File cameraFile;
	private File cropFile;
	private String base64 = "";

	public static void startActivity(Context context) {
		context.startActivity(new Intent(context, SubmitPhotoActivity.class));
	}

	@Override
	public int bindLayout() {
		// TODO Auto-generated method stub
		return R.layout.submit_photo;
	}

	@Override
	public void initView(View view) {
		action_title.setText(R.string.tab1);
		action_right.setText(R.string.submit);
		mine_head = $(R.id.mine_head);
		head_img = $(R.id.head_img);
		titlEditText = $(R.id.title);
		contentEditText = $(R.id.content);

		cropFile = new File(Environment.getExternalStorageDirectory(), "jpg");
	}

	@Override
	public void initData(Context mContext) {

	}

	@Override
	public void initListener() {
		action_back.setOnClickListener(this);
		action_right.setOnClickListener(this);
		mine_head.setOnClickListener(this);
	}

	@Override
	public void resume() {

	}

	@Override
	public void destroy() {
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.action_back:
			AppManager.getAppManager().finishActivity();
			break;

		case R.id.mine_head:
			cameraFile = DialogUtils.showImageDialog(this);
			break;

		case R.id.action_right:
			String title = titlEditText.getText().toString().trim();
			String content = contentEditText.getText().toString().trim();
			if (StringTool.isNoBlankAndNoNull(base64)
					&& StringTool.isNoBlankAndNoNull(title)
					&& StringTool.isNoBlankAndNoNull(content)) {
				if (mApplication.isLogin()) {
					loading.setVisibility(View.VISIBLE);
					updateUser(mApplication.getUserBean().getUserName(),
							base64, title, content);
				} else {
					ToastTool.toastMessage(getContext(), "请先登录！");
				}

			} else {
				ToastTool.toastMessage(getContext(), "请上传照片，并输入 标题和内容！");
			}
			break;
		default:
			break;
		}
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		// 拍照
		if (requestCode == DialogUtils.REQUEST_CODE_CAMERA
				&& FileUtil.isValid(cameraFile) && resultCode == RESULT_OK) {
			DialogUtils.startPhotoCrop(this, Uri.fromFile(cameraFile), 80,
					cropFile);
			cameraFile = null;
			// 系统图片库-选择图片
		} else if (requestCode == DialogUtils.REQUEST_CODE_IMAGE
				&& data != null && resultCode == RESULT_OK) {
			DialogUtils.startPhotoCrop(this, data.getData(), 80, cropFile);
			// 裁剪图片
		} else if (requestCode == DialogUtils.REQUEST_CODE_CROP && data != null
				&& resultCode == RESULT_OK) {
			Bitmap bitmap = BitmapFactory
					.decodeFile(cropFile.getAbsolutePath());
			if (bitmap != null) {
				// 设置妈妈头像
				head_img.setImageBitmap(bitmap);
				base64 = DialogUtils.imgToBase64(bitmap);

			} else {
				ToastTool.toastMessage(this, "用户存储空间不足，请整理空间后上传!");
			}
		}
	}

	/**
	 * 修改个人信息
	 * 
	 * @param loginKey
	 * @param str
	 */
	private void updateUser(String loginKey, String pic, String title,
			String content) {

		Map maps = new HashMap<String, String>();
		maps.put("userName", loginKey);
		maps.put("title", title);
		maps.put("content", content);
		maps.put("image", pic);

		HttpTool.post(Apis.HOST + Apis.REPORT, maps,
				new StringHandler(loading) {

					@Override
					public void success(String response) {
						if (StringTool.isNoBlankAndNoNull(response)) {
							StatusResponse bean = JSON.decode(response,
									StatusResponse.class);
							if (bean != null) {
								String status = bean.getStatus();
								if (StringTool.isNoBlankAndNoNull(status)
										&& "0".equalsIgnoreCase(status)) {
									ToastTool.toastMessage(getContext(),
											bean.getMessage());
									AppManager.getAppManager().finishActivity();
								} else {
									ToastTool.toastMessage(getContext(),
											bean.getMessage());
								}
							}
						}
						loading.setVisibility(View.GONE);
					}

					@Override
					public void failure(int statusCode, String responseBody,
							Throwable e) {

					}
				}, "utf-8");

	}
}
